/**
 * Created by Administrator on 2018/12/7.
 */
//初始化 公司 名稱

// $CertusJsonAjax.ajax("listIsTrueDepartment",{},function(res){
//     if(res && res.data){
//         $$scope.dropdown1List = res.data;
//     }
// })

$CertusFormAjax.ajax("listIsTrueDepartment",{"areaSelect":"XYGRZ"},function(res){
    if(res && res.data){
        $$scope.dropdown1List = res.data;
    }
})

$$scope.sfzddnSelect = [
    {name:"待定",code:"不一定带"},
    {name:"带",code:"带"},
    {name:"不带",code:"不带"},
]

//初始化 公司 名稱
$$scope.rzcomSelectIdSelect1 = [
    {departmentName:"赛特斯集团",departmentCode:"STSJT"},
    // {name:"赛特斯广东",code:"STSGD"},
    // {name:"赛特斯南京",code:"STSNJ"},
    // {name:"赛特斯北京",code:"STSBJ"},
    // {name:"赛特斯上海",code:"STSSH"},
    // {name:"浩方科技",code:"HFKJ"},
    // {name:"浩方信息",code:"HFXX"}
]

// $$scope.belongCompanyCodeSelect = [
//     {name:"赛特斯广东",code:"STSGD"},
//     {name:"赛特斯南京",code:"STSNJ"},
//     {name:"赛特斯北京",code:"STSBJ"},
//     {name:"赛特斯上海",code:"STSSH"},
//     {name:"融合运维事业部",code:"SYB-RX"},
//     {name:"浩方科技",code:"HFKJ"},
//     {name:"浩方信息",code:"HFXX"}
// ]
debugger;
$CertusFormAjax.ajax("stscompSelectByAutho", {"areaSelect":"XYGRZ"}, function (res) {
    $$scope.belongCompanyCodeSelect = res.data;
    $$scope.companySelect = res.data;
})


$$scope.getBelongCompanyName = function(companyCode){
    $CertusFormAjax.ajax("getRealDept",{"companyCode":companyCode},function(res){
        $$scope.rzlcStepEntity.baseUser.belongCompanyName = res.data;
    })
}

$$scope.companyChange = function(a,i){
    for(var ii in $$scope["rzcomSelectIdSelect"+i]){
        if($$scope["rzcomSelectIdSelect"+i][ii].departmentCode == $($("#rzcomSelectId"+i)).find("select").val()){
            $$scope.rzlcStepEntity.departmentName = $$scope["rzcomSelectIdSelect"+i][ii].departmentName
        }
    }
    $CertusFormAjax.ajax("subDepartment",{"superiorDepartCode":a},function(res){
        if(res.data){

            //判断 长度
            if(res.data.length && res.data.length > 0){
                var $rzcomSelect = $(".rzcomSelect");
                for(var j=i+1;j<$rzcomSelect.length+1;j++){
                    $("#rzcomSelectId"+j).remove()
                }
                var id = "rzcomSelectId"+(i+1);
                var idSelect = "rzcomSelectIdSelect"+(i+1);
                $$scope[idSelect] = res.data;
                for(var ii in $$scope[idSelect]){
                    if($$scope[idSelect][ii].departmentCode == $$scope.rzlcStepEntity.baseUser.departmentUser.departmentCode){
                        $$scope.rzlcStepEntity.departmentName = $$scope[idSelect][ii].departmentName
                    }
                }

                var str = "<div class='form-group customerForm rzcomSelect' id='"+ id +"'>\n" +
                    "                    <label class='col-sm-2 control-label'>L"+(i+1)+"</label>\n" +
                    "                        <div class='col-sm-10'>\n" +
                    "                        <select class='form-control'  ng-model='rzlcStepEntity" +
                    ".baseUser.belongCompanyCode"+(i+1)+"' ng-change='companyChange(rzlcStepEntity.baseUser.belongCompanyCode"+(i+1)+"," + (i+1) +")" +
                    "'>\n" +
                    "                        <option ng-repeat='cps in "+ idSelect+"' value='{{cps.departmentCode}}'>{{cps" +
                    ".departmentName}}</option>\n" +
                    "                    </select>\n" +
                    "                    </div>\n" +
                    "                    </div>";

                var a2 = angular.element(str);
                var newVar = $$compile(a2)($$scope);
                angular.element("#rzcomSelectId"+i).after(newVar);
                // angular.element(str);
                //清空 后边 兄弟 节点
                $rzcomSelect.length


            }
        }
    })
}

// $$scope.getBelongCompanyName = function(companyCode){
//     if(companyCode == "STSJT"){
//         return "赛特斯集团";
//     }else if(companyCode == "STSGD"){
//         return "赛特斯广东";
//     }else if(companyCode == "HFKJ"){
//         return "浩方科技";
//     }else if(companyCode == "HFXX"){
//         return "浩方信息";
//     }else if(companyCode == "STSBJ"){
//         return "赛特斯北京";
//     }else if(companyCode == "STSNJ"){
//         return "赛特斯南京";
//     }else if(companyCode == "STSSH"){
//         return "赛特斯上海";
//     }
// }

$CertusJsonAjax.ajax("queryYGRZProcessTrace",{
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
},function(res){

    $$scope.processCCBXTrace = res.data;
})
//性别 select
$$scope.sexSelect = [
    {name:"男",code:"1"},
    {name:"女",code:"0"}
]

//结婚情况
$$scope.marriageSelect = [
    {name:"已婚",code:"1"},
    {name:"未婚",code:"0"},
    {name:"已婚已育",code:"2"}
]

$$scope.getMarriage = function(a){
    if(a == 1){
        return "已婚";
    }else if(a == 0 ){
        return "未婚";
    }else if(a == 2){
        return "已婚已育";
    }

    return "未婚";
}
//初始化 内容
$CertusJsonAjax.ajax("baseUserByProcessId",{
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
},function(res){
    $$scope.rzlcStepEntity = res.data;

    $$scope.rzlcStepEntity.stepInstanceId = $CertusJsonAjax.getStepInstanceId();

    if($$scope.rzlcStepEntity.baseUser.firstWorkTime){
        $$scope.rzlcStepEntity.baseUser.firstWorkTime = $$scope.rzlcStepEntity.baseUser.firstWorkTime.split(" ")[0]
    }

    // $$scope.rzlcStepEntity.baseUser.departmentUser

    var departmentCode = res.data.baseUser.departmentUser.departmentCode;
    $CertusFormAjax.ajax("big2small",{deptcode:departmentCode},function(res){
        var length = res.data.length;

        for(var i = length-1; i>-1; i--){
            j = length - i;
            $$scope.rzlcStepEntity.baseUser["belongCompanyCode"+j] = res.data[i]
            $$scope.companyChange( res.data[i],j);
        }
    })

    $$scope.getBelongCompanyName( $$scope.rzlcStepEntity.baseUser.belongCompanyCode);
    $$scope.rzlcStepEntity.baseUser.sex=parseInt($$scope.rzlcStepEntity.baseUser.sex)+"";
    $$scope.rzlcStepEntity.baseUser.isMarriage=parseInt($$scope.rzlcStepEntity.baseUser.isMarriage)+"";



    //部门 名称 修改
    $CertusFormAjax.ajax("deptedList",{companyCode:$$scope.rzlcStepEntity.baseUser.departmentUser.companyCode},function(res){
        $$scope.departmentSelect =eval("("+res.data+")");
    })
})


var userPinin;
function isSamePinyin(element){
    if(userPinin){
        userPinin = userPinin.toUpperCase();
    }
    var pinyin = "";
    var userName2 = "";
    var userName = "";
    if(element.pinyin){
        pinyin = element.pinyin.toUpperCase();
    }
    if(element.userName2){
        userName2 =  element.userName2.toUpperCase();
    }
    if(element.userName){
        userName = element.userName.toUpperCase();
    }

    //部门名称 联动 查询
    return pinyin.indexOf(userPinin) > -1 || userName2.indexOf(userPinin) > -1 || userName.indexOf(userPinin) > -1;
}


$$scope.departmentChange = function(a){
    for(var xxxx in $$scope.departmentSelect){
        if($$scope.departmentSelect[xxxx].departmentCode == $$scope.rzlcStepEntity.baseUser.departmentUser.departmentCode){
            $$scope.rzlcStepEntity.baseUser.departmentUser.departmentName = $$scope.departmentSelect[xxxx].departmentName;
        }
    }
}
//领导者 姓名变化
$$scope.$watch('rzlcStepEntity.baseUser.leaderName',function(principalName,oldName,b){
    if (principalName != "") {
        var userSelect = $$scope.leaderNameSelect;
        if(principalName && principalName.indexOf(oldName) > -1 && b.leaderNameSelect && b.leaderNameSelect.length > 0){
            userPinin = principalName;
            b.leaderNameSelect = b.leaderNameSelect.filter(isSamePinyin);
        }else{
            $CertusFormAjax.ajax("relatedUser", {name: principalName}, function (res) {
                if (res.data && res.data.length > 0) {
                    b.leaderNameSelect = res.data;
                }
            });
        }

        if (b.leaderNameSelect && b.leaderNameSelect.length == 1) {
            var userSelect2 = userSelect[0];
            $$scope.rzlcStepEntity.baseUser.leaderName = userSelect2.userName;
            $$scope.rzlcStepEntity.baseUser.leaderId = userSelect2.userId;
        }
    }
});



//获得 员工 性别 名
$$scope.getSex = function(a){
    if(a == 1){
        return "男";
    }else{
        return "女";
    }
}



//自动 筛选 面试 人
$$scope.$watch('rzlcStepEntity.interviewerManName',function(principalName,oldName,b){
    if (principalName != "") {
        var userSelect = $$scope.interviewerNameSelect;
        if(principalName && principalName.indexOf(oldName) > -1 && b.interviewerNameSelect && b.interviewerNameSelect.length > 0){
            userPinin = principalName;
            b.interviewerNameSelect = b.interviewerNameSelect.filter(isSamePinyin);
        }else{
            $CertusFormAjax.ajax("relatedUser", {name: principalName}, function (res) {
                if (res.data && res.data.length > 0) {
                    b.interviewerNameSelect = res.data;
                }
            });
        }
        if (b.interviewerNameSelect && b.interviewerNameSelect.length == 1) {
            var userSelect2 = userSelect[0];
            $$scope.rzlcStepEntity.interviewerManName = userSelect2.userName;
            $$scope.rzlcStepEntity.interviewerManId = userSelect2.userId;
        }
    }
});

//添加 公司
$$scope.addUserSubmit = function(){
    if(!$$scope.rzlcStepEntity.baseUser.leaderId){
        layer.msg("直接领导 填写不正确");
    }
    // if(!$$scope.rzlcStepEntity.interviewerManId){
    //     layer.msg("面试人员未填写");
    // }

    if($$scope.rzlcStepEntity.baseUser.willHaveEmail != 0 && $$scope.rzlcStepEntity.baseUser.willHaveEmail != 1){
        layer.msg("请选择是否需要开通企业邮箱");
        return;
    }


    if($$scope.rzlcStepEntity.baseUser.willHaveWeChat != 0 && $$scope.rzlcStepEntity.baseUser.willHaveWeChat != 1){
        layer.msg("请选择是否需要开通企业微信");
        return;
    }

    if($$scope.rzlcStepEntity.baseUser.willHaveBpm != 0 && $$scope.rzlcStepEntity.baseUser.willHaveBpm != 1){
        layer.msg("请选择是否需要开通bpm");
        return;
    }

    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if(!myreg.test($$scope.rzlcStepEntity.baseUser.personalEmail)){
        layer.msg("邮箱格式不正确 请检查")
        return false;
    }

    //获取 所选 department code
    var $rzcomSelect = $(".rzcomSelect");
    $$scope.rzlcStepEntity.baseUser.departmentUser = {};
    if($rzcomSelect.length){
        for(var i = 0;i < $rzcomSelect.length;i++){
            var selectValue = $($rzcomSelect[i]).find("select").val();
            if(selectValue && !(selectValue.indexOf("undefined") > -1)){
                $$scope.rzlcStepEntity.baseUser.departmentUser.departmentCode = selectValue;
            }
        }
    }else{
        if($($rzcomSelect).find("select").val()){
            $$scope.rzlcStepEntity.baseUser.departmentUser.departmentCode = $($rzcomSelect).find("select").val();
        }else{
            layer.msg("请选择入职部门");
            return;
        }
    }
    //检查 非空
    if(certusValidateNotnull()){
        $CertusJsonAjax.ajax("rzlcStepStart",$$scope.rzlcStepEntity,function(res){
            if(res.data == "success"){
                findBacklogPersonHead($CertusFormAjax, "RZLC");
                history.back();
                return;
            }
            layer.msg("数据不全，请检查");
        })
    }
}

function dep1_name_change(a){
    debugger;
    $$scope.rzlcStepEntity.baseUser.dep1_name = $(a).html();
    $("#dropdownMenu1").html($$scope.rzlcStepEntity.baseUser.dep1_name);
}

function dep2_name_change(a){
    debugger;
    $$scope.rzlcStepEntity.baseUser.dep2_name = $(a).html();
    $("#dropdownMenu2").html($$scope.rzlcStepEntity.baseUser.dep2_name);
}